package com.lx.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lx.yygh.cmn.client.DictFeignClient;
import com.lx.yygh.enums.DictEnum;
import com.lx.yygh.hosp.repository.HospitalRepository;
import com.lx.yygh.hosp.service.HospitalService;
import com.lx.yygh.model.hosp.Hospital;
import com.lx.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author c先生
 * @date 2022年10月15日 下午 4:24:55
 * @describe
 */
@Service
public class HospitalServiceImpl implements HospitalService {
    @Resource
    HospitalRepository hospitalRepository;
    @Resource
    DictFeignClient dictFeignClient;

    @Override
    public Boolean saveHospital(Map<String, Object> map) {
        //map转为对象
        Hospital hospital = JSONObject.parseObject(JSONObject.toJSONString(map), Hospital.class);
        //查询mongodb中是否已经有了医院信息
        Hospital hospitalByHoscode = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());
        if(null!=hospitalByHoscode){
            //不为空，进行修改
            hospital.setUpdateTime(new Date());
            hospital.setCreateTime(hospitalByHoscode.getCreateTime());
            hospital.setIsDeleted(0);
            hospital.setStatus(hospitalByHoscode.getStatus());
            hospitalRepository.save(hospital);
            return true;
        }else {
            //为空，进行添加
            Date date = new Date();
            hospital.setCreateTime(date);
            hospital.setUpdateTime(date);
            hospital.setIsDeleted(0);
            hospital.setStatus(0);
            hospitalRepository.save(hospital);
            return true;
        }
    }

    @Override
    public Hospital getHospitalByHoscode(String hoscode) {
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    @Override
    public Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //0 为第一页
        Pageable pageable = PageRequest.of(page-1,limit);
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        //创建匹配器对象，即如何使用查询条件
        ExampleMatcher exampleMatcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase();
        Example<Hospital> example = Example.of(hospital,exampleMatcher);
        Page<Hospital> all = hospitalRepository.findAll(example, pageable);
        all.getContent().stream().forEach(item->{
            //把code值转化为名字
            this.packHospital(item);
        });
        return all;
    }

    private Hospital packHospital(Hospital hospital) {
        String hosTypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(), hospital.getHostype());
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        hospital.getParam().put("hostypeString",hosTypeString);
        hospital.getParam().put("fullAddress",provinceString+cityString+districtString+hospital.getAddress());
        return hospital;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.getHospitalById(id);
        hospital.setStatus(status);
        //不为空，进行修改
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Hospital getHospById(String id) {
        Hospital hospital = hospitalRepository.getHospitalById(id);
        Hospital hospital1 = hospital = this.packHospital(hospital);
        return hospital1;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital.getHosname();
    }

    @Override
    public List<Hospital> getByHosname(String queryString) {
        List<Hospital> hospitalList = hospitalRepository.getHospitalByHosnameLike(queryString);
        return hospitalList;
    }
}

